﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;

namespace BPMS.Utility
{
    public class StringHelper
    {

        /// <summary>查找字符位置</summary>
        public static int IndexOf(StringBuilder sb, char value)
        {
            for (int i = 0; i < sb.Length; i++)
                if (sb[i] == value) return i;
            return -1;
        }

        /// <summary>是否包含</summary>
        public static bool Contains(string source, string value, StringComparison comparisonType)
        {
            return source.IndexOf(value, comparisonType) != -1;
        }
        /// <summary>是否包含全部</summary>
        public static bool ContainsAll(string source, params string[] values)
        {
            foreach (string value in values)
                if (!source.Contains(value)) return false;
            return true;
        }
        /// <summary>是否包含全部</summary>
        public static bool ContainsAll(string source, params char[] values)
        {
            foreach (char value in values)
                if (!source.Contains(value)) return false;
            return true;
        }
        /// <summary>是否包含任意一个</summary>
        public static bool ContainsAny(string source, params string[] values)
        {
            foreach (string value in values)
                if (source.Contains(value)) return true;
            return false;
        }
        /// <summary>是否包含任意一个</summary>
        public static bool ContainsAny(string source, params char[] values)
        {
            foreach (char value in values)
                if (source.Contains(value)) return true;
            return false;
        }
        /// <summary>是否包含任意一个</summary>
        public static bool ContainsAny(string source, IEnumerable<string> values)
        {
            foreach (string value in values)
            {
                if (string.IsNullOrEmpty(value)) continue;
                if (source.Contains(value)) return true;
            }
            return false;
        }

        /// <summary>包含此字符数</summary>
        public static int CharacterCount(string s, char c)
        {
            return (from x in s.ToCharArray() where x == c select x).Count();
        }
        /// <summary>获取字符串长度，按中文2位，英文1位进行计算</summary>
        public static int CharCodeLength(string source)
        {
            int n = 0;
            foreach (var c in source.ToCharArray())
                if (c < 128)
                    n++;
                else
                    n += 2;
            return n;
        }
        /// <summary>两端拼接双引号</summary>
        public static string AddQuotes(string s)
        {
            return string.Concat("\"", s, "\"");
        }
        /// <summary>拼接</summary>
        public static string Append(string s, params string[] values)
        {
            string[] items = new string[values.Length + 1];
            items[0] = s;
            values.CopyTo(items, 1);
            return string.Concat(items);
        }
        /// <summary>向后拼接</summary>
        public static string Append(string s, params object[] values)
        {
            object[] items = new object[values.Length + 1];
            items[0] = s;
            values.CopyTo(items, 1);
            return string.Concat(items);
        }
        /// <summary>向前拼接</summary>
        public static string Prepend(string s, params string[] values)
        {
            string[] items = new string[values.Length + 1];
            values.CopyTo(items, 0);
            items[items.Length - 1] = s;
            return string.Concat(items);
        }
        /// <summary>向前拼接</summary>
        public static string Prepend(string s, params object[] values)
        {
            object[] items = new object[values.Length + 1];
            values.CopyTo(items, 0);
            items[items.Length - 1] = s;
            return string.Concat(items);
        }
        /// <summary>取两个字符之间的内容</summary>
        public static string Between(string s, char left, char right)
        {
            int indexFrom = s.IndexOf(left);
            if (indexFrom != -1)
            {
                ++indexFrom;
                int indexTo = s.IndexOf(right, indexFrom);
                if (indexTo != -1) return s.Substring(indexFrom, indexTo - indexFrom);
            }
            return string.Empty;
        }
        /// <summary>取字符之左边的内容</summary>
        public static string LeftOf(string s, char c)
        {
            int index = s.IndexOf(c);
            return index != -1 ? s.Substring(0, index) : s;
        }
        /// <summary>取字符之左边的几个内容</summary>
        public static string LeftOf(string s, char c, int n)
        {
            int index = -1;
            while (n > 0)
            {
                index = s.IndexOf(c, index + 1);
                if (index == -1) break;
                --n;
            }
            return index != -1 ? s.Substring(0, index) : s;
        }
        /// <summary>取字符之右边的内容</summary>
        public static string RightOf(string s, char c)
        {
            int index = s.IndexOf(c);
            return index != -1 ? s.Substring(index + 1) : s;
        }
        /// <summary>取字符之右边的几个内容</summary>
        public static string RightOf(string s, char c, int n)
        {
            int index = -1;
            while (n > 0)
            {
                index = s.IndexOf(c, index + 1);
                if (index == -1)
                { break; }
                --n;
            }
            return index != -1 ? s.Substring(index + 1) : s;
        }

        /// <summary>把一个字符串从后面截取一定位数,并在前面添加相应字符串
        /// 如123456789,截取最后4位,前面添加****,最终结果为****6789</summary>
        /// <param name="s">字符串</param>
        /// <param name="subLength">要保留最后几位</param>
        /// <param name="aboveWord">前面要添加的字符串(可不填)</param>
        /// <returns></returns>
        public static string ShowSplits(object s, int subLength, string aboveWord = "")
        {
            string word = s.ToString();
            if (word.Length > subLength)
            {
                return aboveWord + word.Substring(word.Length - subLength);
            }
            else
            {
                return word;
            }
        }

        /// <summary>
        /// 字符串必须以"/"符号结尾
        /// </summary>
        /// <param name="input"></param>
        /// <returns></returns>
        public static string LastBacklash(string input)
        {
            if (input == null)
                return input;
            if (!input.EndsWith("/"))
                return input + "/";
            else
                return input;
        }
    }
}
